<?php

namespace App\Http\Controllers;

use App\Repositories\MoneyLogRepository;
use Illuminate\Http\Request;
use App\Http\Controllers\BaseController;
use App\Models\MoneyLogModel;

class MoneyLogController extends BaseController
{
    /**
     * @param Request $request
     * @param MoneyLogRepository $moneyRepository
     * @return \Illuminate\Http\JsonResponse
     */
    public function list (Request $request, MoneyLogRepository $moneyRepository)
    {
        $params = $request->all();
        $list = @$moneyRepository->getList($params);
        $moneys = @$moneyRepository->getBalance(true, true);
        $list['balance'] = $moneys['balance'];
        $list['bankIn'] = $moneys['bankIn'];
        $list['bankOut'] = $moneys['bankOut'];
        $list['unfinishedMoney'] = @$moneyRepository->getUnfinishedMoney();
        return $this->success($list);
    }

    public function getTyps () {
        return $this->success(MoneyLogModel::TYPES);
    }

    public function getMoneyDetail (Request $request) {
        $id = $request->input('id');
        $money = @MoneyLogModel::query()->find($id);
        return $this->success($money);
    }

    /**
     * @param Request $request
     * @param MoneyLogRepository $moneyRepository
     * @return \Illuminate\Http\JsonResponse
     */
    public function save (Request $request, MoneyLogRepository $moneyRepository)
    {
        $allParams = $request->all();
        $rst = @$moneyRepository->save($allParams);
        if ($rst) {
            return $this->success([]);
        }
        return $this->error(405, '保存失败，请稍后再试');
    }
}
